package cn.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/encode")
public class encode implements Filter {
	//初始化参数
    private String characterEncoding = null;

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding(characterEncoding);
		response.setCharacterEncoding(characterEncoding);
		response.setContentType("text/html;charset="+characterEncoding);
		/*
		 * response.addHeader("Access-Control-Allow-Origin", "*");
		 * response.addHeader("Access-Control-Allow-Methods",
		 * "GET, POST, PUT, DELETE, OPTIONS");
		 * response.addHeader("Access-Control-Allow-Headers",
		 * "Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,token"
		 * );
		 */
		//获取URL的路径和参数
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse rep = (HttpServletResponse) response;
		/*
		 * String path = req.getServletPath(); String param = req.getQueryString();
		 * if(path!=null&&param!=null) { path = path+"?"+param; }
		 */
		//System.out.println("Path:"+path);
		//跳转或带参数跳转
		//request.getRequestDispatcher(path).forward(request, response);
		// 把请求传回过滤链
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		characterEncoding = fConfig.getInitParameter("characterEncoding");
	}

}
